package com.justgame.project.base.interceptor;

import com.justgame.project.base.annotation.VerificationCode;
import com.justgame.project.cloud.common.constant.Const;
import com.justgame.project.cloud.common.exception.Exc;
import com.justgame.project.cloud.common.util.Encryption;
import com.justgame.project.cloud.common.util.ObjUtil;
import com.justgame.project.cloud.common.util.RedisUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * VerificationCode（验证码注解） Controller拦截器
 */
@Component
@AllArgsConstructor
public class VerificationCodeInterceptor implements HandlerInterceptor {
    @Resource
    private final RedisUtil redisUtil;

    private boolean isValidate(Object handler) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        VerificationCode annotation = method.getAnnotation(VerificationCode.class);
        if (annotation != null) {
            return ((VerificationCode) annotation).value();
        } else {
            return false;
        }
    }

    private boolean ValidateRequest(HttpServletRequest request) {
        String SessionCode, HeaderCode;
        try {
            HeaderCode = request.getHeader(Const.KEY_VERIFICATION_CODE_2WEB).toLowerCase();

            if (StringUtils.isBlank(HeaderCode)) {
                throw new Exc("验证码不能为空", Const.RES_ERROR_BAD_VERIFICATION_CODE);
            }
            StringBuffer sb = Encryption.PasswordMD5(HeaderCode, Const.DEFAULT_SALT_VERIFICATION);
            if (StringUtils.isNotBlank(HeaderCode)) {
                SessionCode = String.valueOf(request.getSession().getAttribute(Const.KEY_VERIFICATION_CODE_2WEB));
                if (ObjUtil.isNull(SessionCode)) {
                    SessionCode = redisUtil.get(request.getSession().getId());
                }
                if (SessionCode.equals(String.valueOf(sb))) {
                    redisUtil.delete(request.getSession().getId());
                    return true;
                } else {
                    throw new Exc(Const.RES_ERROR_BAD_VERIFICATION_CODE_CHINESE, Const.RES_ERROR_BAD_VERIFICATION_CODE);
                }
            }
            throw new Exc("验证码不能为空，请输入验证码。", Const.RES_ERROR_BAD_VERIFICATION_CODE);
        } catch (NullPointerException nullPointer) {
            throw new Exc("验证码不能为空", Const.RES_ERROR_BAD_VERIFICATION_CODE);
        }
    }


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            if (isValidate(handler)) {
                return ValidateRequest(request);
            }
        }
        return true;

    }
}
